package gapidt;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.ow2.easywsdl.schema.SchemaFactory;
import org.ow2.easywsdl.schema.api.Schema;
import org.ow2.easywsdl.schema.api.SchemaException;
import org.ow2.easywsdl.tooling.java2wsdl.JavaToEasyWSDL;
import org.ow2.easywsdl.tooling.java2wsdl.WSDLGenerationContext;
import org.ow2.easywsdl.wsdl.WSDLFactory;
import org.ow2.easywsdl.wsdl.api.Description;
import org.ow2.easywsdl.wsdl.api.Types;
import org.ow2.easywsdl.wsdl.api.WSDLException;
import org.w3c.dom.Document;

public class EasyWSDLTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		List<String> polymorphClasses = new ArrayList<String>();
		
		List<Class<?>> classes = new ArrayList<Class<?>>();
		classes.add(TestClass.class);
		
		JavaToEasyWSDL javaToWsdl = new JavaToEasyWSDL(true);
		try {
			WSDLGenerationContext genCtx = javaToWsdl.generateWSDL(classes, false, polymorphClasses);
			Description wsdl  = genCtx.getDescription();
			Schema schema  = genCtx.getSchema(wsdl.getTargetNamespace());
			Types types = wsdl.createTypes();
			types.addSchema(schema);
			wsdl.setTypes(types);
			
			// Write a WSDL 1.1 or 2.0 (depend of desc version)
			Document doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(wsdl);
			Document schemaDoc = SchemaFactory.newInstance().newSchemaWriter().getDocument(schema);
			
			
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();
			StringWriter buffer = new StringWriter();
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
			
			transformer.transform(new DOMSource(doc), new StreamResult(buffer));
			String wsdlStr = buffer.toString();
			
			System.out.println(wsdlStr);
			//System.out.println(schemaStr);
			
		} catch (WSDLException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SchemaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
